package cn.com.dycc.party_dues.pay_online.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.page.BaseParam;
import cn.com.dycc.party_dues.info.service.Pb_Party_DuesNotFoundException;
import cn.com.dycc.party_dues.pay_online.dao.Pb_Party_Dues_Pay_OnlineDao;
import cn.com.dycc.party_dues.pay_online.param.Pb_Party_Dues_Pay_OnlineParam;
import cn.com.dycc.util.ConfigRegex;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;


@Service("pb_Party_Dues_Pay_OnlineService")
@Transactional
public class Pb_Party_Dues_Pay_OnlineServiceImpl implements Pb_Party_Dues_Pay_OnlineService{

	@Resource
	private Pb_Party_Dues_Pay_OnlineDao pb_Party_Dues_Pay_OnlineDao;
	
	
	public int add(BaseParam param, HttpServletRequest request) throws Exception {
		
		return 0;
	}

	public int delete(String[] attrStrs, HttpServletRequest request) throws Exception {
		
		return 0;
	}

	public int update(BaseParam param, HttpServletRequest request) throws Exception {
		
		return 0;
	}

	public JsonObjectResult findByParam(BaseParam param, HttpServletRequest request) throws Exception {
		Pb_Party_Dues_Pay_OnlineParam param0=(Pb_Party_Dues_Pay_OnlineParam)param;
		List<Object> lists = new ArrayList<Object>();
		checkParam(param0);
		param0 = (Pb_Party_Dues_Pay_OnlineParam)realParam(param0, request);
		String count = pb_Party_Dues_Pay_OnlineDao.getCountByParam(param0);

		if ("0".equals(count)) {
			return new JsonObjectResult(lists);
		}
		
		param0=(Pb_Party_Dues_Pay_OnlineParam)PageObjectUtil.getMysqlPageObject(count, param0);
		lists = pb_Party_Dues_Pay_OnlineDao.findByParam(param0);
		return new JsonObjectResult(count,lists);
		
	}
	
	private void checkParam(BaseParam param) {
		Pb_Party_Dues_Pay_OnlineParam param0=(Pb_Party_Dues_Pay_OnlineParam)param;
		if(param0==null){
			throw new Pb_Party_DuesNotFoundException("未登录或登录超时,请重新登录");
		}
		if(param0.getDues_month()!=null&&!param0.getDues_month().trim().isEmpty()){
			if(!param0.getDues_month().matches(ConfigRegex.DATEREGEX3)){
				throw new Pb_Party_DuesNotFoundException("党费（年）月份格式不正确,应为1990-01");
			}
		}
		
	}
	
	private BaseParam realParam(BaseParam param, HttpServletRequest request) {
		Pb_Party_Dues_Pay_OnlineParam param0=(Pb_Party_Dues_Pay_OnlineParam)param;	
		if(param0.getUser_name()!=null&&!param0.getUser_name().trim().isEmpty()){
			param0.setUser_name(StringUtil.format_mh_str(param0.getUser_name()));
		}
		if(param0.getParty_dept_name()!=null&&!param0.getParty_dept_name().trim().isEmpty()){
			param0.setParty_dept_name(StringUtil.format_mh_str(param0.getParty_dept_name()));
		}
		if(param0.getDept_name()!=null&&!param0.getDept_name().trim().isEmpty()){
			param0.setDept_name(StringUtil.format_mh_str(param0.getDept_name()));
		}
		if(param0.getPay_des()!=null&&!param0.getPay_des().trim().isEmpty()){
			param0.setPay_des(StringUtil.format_mh_str(param0.getPay_des()));
		}
		
		if(param0.getOrder()==null||param0.getOrder().isEmpty()||"1".equals(param0.getOrder())){
			param0.setOrder("p.create_date desc");
		}else if("2".equals(param0.getOrder())){
			param0.setOrder("p.create_date asc");
			
		}else{
			param0.setOrder(null);//手动防止SQL注入
		}
		
		return param0;
	}

}
